package org.jeecg.modules.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.modules.client.ClientUrlConstant;
import org.jeecg.modules.client.HttpClient;
import org.jeecg.modules.service.DeviceService;
import org.jeecg.modules.dto.device.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author Administrator
 */
@Service
public class DeviceServiceImpl implements DeviceService {

    private static final Logger log_server = LoggerFactory.getLogger("netty-info");

    @Value("${application.openUrl}")
    private String httpUrl;

    /**
     * 远程开关锁
     *
     * @param switchLockReq
     */
    @Override
    public Result<?> switchLock(SwitchLockReq switchLockReq) {
        log_server.info("远程开关锁下发 ========== :{}", switchLockReq);
        Result<?> resResult = HttpClient.doClassPost(httpUrl + ClientUrlConstant.switch_lock, switchLockReq);
        if (resResult.getCode() != CommonConstant.SC_OK_200){
            return Result.error("请求失败，网点已断线！");
        }
        JSONObject jsonObject = JSON.parseObject((String) resResult.getResult());
        Integer code = (Integer) jsonObject.get("code");
        String message =(String) jsonObject.get("message");
        if (code==200){
            return Result.OK(message);
        }else {
            return Result.error(message);
        }
    }

    /**
     * 车桩状态查询
     *
     * @param pileQueryReq
     */
    @Override
    public Result<?> pileQuery(PileQueryReq pileQueryReq) {
        log_server.info("车桩状态查询请求参数 ========== :{}", JSON.toJSONString(pileQueryReq));
        Result<?> resResult = HttpClient.doClassPost(httpUrl + ClientUrlConstant.pile_query, pileQueryReq);
        if (resResult.getCode() != CommonConstant.SC_OK_200){
            return Result.error("请求失败，网点已断线！");
        }
        return resResult;
    }

    /**
     * 车桩默认语音
     * @param setVoiceReq
     */
    @Override
    public Result<?> updateVoice(SetVoiceReq setVoiceReq) {
        log_server.info("车桩默认语音下发 ========== :{}", JSON.toJSONString(setVoiceReq));
        Result<?> resResult = HttpClient.doClassPost(httpUrl + ClientUrlConstant.set_voice, setVoiceReq);
        if (resResult.getCode() != CommonConstant.SC_OK_200){
            return Result.error("请求失败，网点已断线！");
        }
        return resResult;
    }
}
